package com.aos.erp.schools.service.student.imp;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.schools.cutils.Constant;
import com.aos.erp.schools.enums.student.FieldStatus;
import com.aos.erp.schools.enums.student.StudentStatus;
import com.aos.erp.schools.enums.student.SubjectType;
import com.aos.erp.schools.mapper.student.StudentFieldMapper;
import com.aos.erp.schools.mapper.student.StudentMapper;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentField;
import com.aos.erp.schools.model.student.ro.student.SelectByStateRo;
import com.aos.erp.schools.model.student.vo.field.StudentFieldVo;
import com.aos.erp.schools.model.student.vo.student.StudentAllVo;
import com.aos.erp.schools.service.student.StudentFieldService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/2 14:48
 * @Description: 学员成绩考试Serviceimp
 */
@Service
public class StudentFieldServiceImpl extends ServiceImpl<StudentFieldMapper, StudentField> implements StudentFieldService {

    @Resource
    private StudentMapper studentMapper ;

    @Override
    public void saveBean(StudentField studentField) throws Exception {
        Long studentId=studentField.getStudentId();//学员id
        Condition condition = new Condition(StudentField.class);

        if (studentField.getStudentId()!=null && studentField.getExamSubject()!=null){
            condition.eqFiled("studentId", studentField.getStudentId());
            condition.eqFiled("examSubject", studentField.getExamSubject());
        }
        StudentField sf=this.getOne(condition.getWrapper());
        if(sf==null || sf.getId()==0)
        {
                //保存的传来的数据
                studentField.setDegree(1L);//第一次
               studentField.setCreated(new Date());
               this.save(studentField);
        }else
            {
                //修改原来的数据
                studentField.setDegree(sf.getDegree()+1L);
                studentField.setId(sf.getId());
                studentField.setCreated(new Date());
                this.updateById(sf);
            }
         int status= getStatus(studentField.getExamSubject(),studentField.getFieldStatus());
         if(studentId!=null && status!=0)
         {
             studentMapper.updateStudentByStatus(studentId,status);
         }
         //如果studentId=0 没有学员 就不进行修改状态操作
    }

    @Override
    public PageBean<StudentFieldVo> queryFieldBySubject(SelectByStateRo ro) {

        Page<StudentFieldVo> page = new Page<StudentFieldVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentFieldVo> noticeInfoList = baseMapper.queryFieldBySubject(page, ro);
        page.setRecords(noticeInfoList);
        PageBean<StudentFieldVo> bean = new PageBean<StudentFieldVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;

    }

    public int getStatus(Integer subject,FieldStatus studentField)
    {
        int status=0;
        if(Constant.k1Num==subject)  //科目一
        {
        if(FieldStatus.fail.equals(studentField))//失败
        {
            status=StudentStatus.joink1.getValues();//科目一在培
        }
       }
        if(Constant.k2Num==subject)
        {
            if(FieldStatus.fail.equals(studentField))
            {
                status=StudentStatus.joink2.getValues();
            }
        }

        if(Constant.k3Num==subject)
        {
            if(FieldStatus.fail.equals(studentField))
            {
                status=StudentStatus.joink3.getValues();
            }
            if(FieldStatus.ok.equals(studentField))
            {
                status=StudentStatus.joink4.getValues();
            }
        }

        if(Constant.k4Num==subject)
        {
            if(FieldStatus.fail.equals(studentField))
            {
                status=StudentStatus.joink4.getValues();
            }
            if(FieldStatus.ok.equals(studentField))
            {
                status=StudentStatus.edd.getValues();
            }
        }
        return status;
    }

}
